package besteAukera;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.Serializable;
import java.util.Vector;

public class Hanka extends IrudiAdapter implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int x;
	private int y;
	private String mota;
	private int izena;
	private SarreraDatu sd1;
	private IrteeraDatu irt;
	private Txip ch;
	private boolean exekutatuDa = false;
	private Vector<Loturak> lotLista = new Vector<Loturak>();
	private int luzera;
	private int indizea;
	private String logika;
	private boolean loturaDu;
	private String bertEdoHoriz;
	private Color kolorea;
	private String alderantziz;
	private boolean area;
private boolean kontrola;
	// Koordenatuak chip klasetik heredatuko ditu
	public Hanka(int x1, int y1, String log, String m, int izen, Txip txipa,
			String beh, String ald,boolean kon) {
		x = x1;
		y = y1;
		mota = m;
		izena = izen;
		ch = txipa;
		logika = log;
if(logika.compareTo("negatiboa")==0){
		sd1 = new SarreraDatu(x - 8, y - 8,1);}
		else {sd1 = new SarreraDatu(x - 8, y - 8,0);}
if(logika.compareTo("negatiboa")==0){
		irt = new IrteeraDatu(x - 8, y - 8, 1);}
else{irt = new IrteeraDatu(x - 8, y - 8, 0);}
		kontrola=kon;
		exekutatuDa=false;
		luzera = 20;
		
		loturaDu = false;
		bertEdoHoriz = beh;
		kolorea = Color.black;
		alderantziz = ald;
		area = false;

		// TODO Auto-generated constructor stub
	}
public void setBitKopurua(int kopurua){
	if (mota.compareTo("s")==0){
	this.getSarrera().setBitKopurua(kopurua);}
	else {
		this.getIrteera().setBitKopurua(kopurua);
	}
}
public int getBitKopurua(){
	if (this.getHankaMota().compareTo("s")==0)
	return this.getSarrera().bitKopurua;
	else return this.getIrteera().bitKopurua;
}
	// irteerako balioa erakusteko
	public void setExekutatuDa(boolean exec) {
		
		exekutatuDa = exec;
		
	}
	public boolean isKontrolBita(){
		return kontrola;
	}
public void update(Graphics g){
	
}
	public void marraztu(Graphics g) {

		if (area == true) {
			kolorea = Color.red;
//			g.setColor(kolorea);
		}

		else if (area==false){
			if (ch.getAukeratua() == true) {
				kolorea = Color.GRAY;
//				g.setColor(kolorea);
			} else {
				kolorea = Color.black;
//				g.setColor(kolorea);
			}
		}

		g.setColor(kolorea);
		if (alderantziz.compareTo("ez")==0) {
			if (bertEdoHoriz.compareTo("h")==0) {
				if ((logika.compareTo("positiboa") == 0)||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.BusBitarHamartar")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(ch.getClass().toString().compareTo("class besteAukera.BusHamartarBitar")==0)) {
					
					if (mota.compareTo("i") == 0){
						g.setColor(kolorea);
						g.drawLine(x, y, x - 20, y);
						if(getIrteera().getBitKopurua()!=1){
						g.drawLine(x-10, y+2, x-8, y-2);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getIrteera().getBitKopurua()), x-14, y-3);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
					else{
						g.setColor(kolorea);
						g.drawLine(x, y, x + 20, y);
						if(getSarrera().getBitKopurua()!=1){
						g.drawLine(x+8, y+2, x+10, y-2);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x+10, y-3);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
				} else if (logika.compareTo("negatiboa") == 0) {
					if (mota.compareTo("i") == 0) {
						g.setColor(kolorea);
						g.drawLine(x, y, x - 16, y);
						g.drawOval(x - 20, y - 2, 4, 4);
						if(getIrteera().getBitKopurua()!=1){
						g.drawLine(x-10, y+2, x-8, y-2);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getIrteera().getBitKopurua()), x-14, y-3);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					} else {
						g.setColor(kolorea);
						g.drawLine(x, y, x + 16, y);
						g.drawOval(x + 16, y - 2, 4, 4);
						if(getSarrera().getBitKopurua()!=1){
						g.drawLine(x+8, y+2, x+10, y-2);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x+10, y-3);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
				}
			}//bertikala
			else {
				if ((logika.compareTo("positiboa") == 0)||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.SuperTxip2")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(ch.getClass().toString().compareTo("class besteAukera.SuperTxip")==0)) {
					if (mota.compareTo("i") == 0){
						g.setColor(kolorea);
						g.drawLine(x, y, x, y - luzera);
						if(getIrteera().getBitKopurua()!=1){
						g.drawLine(x+2, y-10, x-2, y-8);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getIrteera().getBitKopurua()), x+3, y-10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
					else{
						g.setColor(kolorea);
						g.drawLine(x, y, x, y + luzera);
						if(getSarrera().getBitKopurua()!=1){
						g.drawLine(x+2, y+8, x-2, y+12);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x+3, y+10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
				} else if (logika.compareTo("negatiboa") == 0) {
					if (mota.compareTo("i") == 0) {
						g.setColor(kolorea);
						g.drawLine(x, y, x, y - (luzera - 4));
						g.drawOval(x - 2, y - luzera, 4, 4);
						if(getIrteera().getBitKopurua()!=1){
						g.drawLine(x+2, y-10, x-2, y-8);
					
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getIrteera().getBitKopurua()), x+3, y-10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
						
					} else {
						g.setColor(kolorea);
						g.drawOval(x - 2, y + 16, 4, 4);
						g.drawLine(x, y, x, y + 16);
						if(getSarrera().getBitKopurua()!=1){
						g.drawLine(x+2, y+8, x-2, y+12);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x+3, y+10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
				}

			}
		}//alderantziz 
		else if (alderantziz.compareTo("bai")==0) {
			if (bertEdoHoriz.compareTo("h")==0) {
				if ((logika.compareTo("positiboa") == 0)||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.SuperTxip2")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(ch.getClass().toString().compareTo("class besteAukera.SuperTxip")==0)) {
					if (mota.compareTo("i") == 0){
						g.drawLine(x, y, x + 20, y);
						if(getIrteera().getBitKopurua()!=1){
					g.drawLine(x+10, y+2, x+12, y-2);
					
					Font lehenetsia=g.getFont();
					Font f=new Font("name",Font.BOLD,9);
					g.setFont(f);
					g.drawString(Integer.toString(getIrteera().getBitKopurua()), x+10, y-3);
					g.setFont(lehenetsia);
				 	g.setColor(kolorea);}}
					else{
						g.drawLine(x, y, x - 20, y);
						if(getSarrera().getBitKopurua()!=1){
						g.drawLine(x-12, y+2, x-10, y-2);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x-15, y-2);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
				} else if (logika.compareTo("negatiboa") == 0) {
					if (mota.compareTo("i") == 0) {
						g.drawLine(x, y, x + 16, y);
						g.drawOval(x + 16, y - 2, 4, 4);
						if(getIrteera().getBitKopurua()!=1){
						g.drawLine(x+2, y-10, x-2, y-8);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getIrteera().getBitKopurua()), x+3, y-10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					} else {
						g.drawLine(x, y, x - 16, y);
						g.drawOval(x - 20, y - 2, 4, 4);
						if(getSarrera().getBitKopurua()!=1){
							g.drawLine(x+8, y+2, x+10, y-2);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x+10, y-3);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
				}
			}
//alderantziz bertikala
			else {
				if ((logika.compareTo("positiboa") == 0) ||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(logika.compareTo("null")==0)||(ch.getClass().toString().compareTo("class besteAukera.SuperTxip2")==0)||(ch.getClass().toString().compareTo("class besteAukera.Kablea")==0)||(ch.getClass().toString().compareTo("class besteAukera.SuperTxip")==0)){
					if (mota.compareTo("i") == 0){
						g.drawLine(x, y, x, y + luzera);
						if(getIrteera().getBitKopurua()!=1){
						g.drawLine(x+2, y-10, x-2, y-8);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getIrteera().getBitKopurua()), x+3, y-10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					}
					else{
						g.drawLine(x, y, x, y - luzera);
						if(getSarrera().getBitKopurua()!=1){
						g.drawLine(x+2, y-10, x-2, y-8);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x+3, y-10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}}
				} else if (logika.compareTo("negatiboa") == 0) {
					if (mota.compareTo("i") == 0) {
						g.drawOval(x - 2, y + 16, 4, 4);
						g.drawLine(x, y, x, y + 16);
						if(getIrteera().getBitKopurua()!=1){
						g.drawLine(x+2, y-10, x-2, y-8);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getIrteera().getBitKopurua()), x+3, y-10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);}
					} else {
						g.drawLine(x, y, x, y - (luzera - 4));
						g.drawOval(x - 2, y - luzera, 4, 4);
						if(getSarrera().getBitKopurua()!=1){
						g.drawLine(x+2, y-10, x-2, y-8);
						
						Font lehenetsia=g.getFont();
						Font f=new Font("name",Font.BOLD,9);
						g.setFont(f);
						g.drawString(Integer.toString(getSarrera().getBitKopurua()), x+3, y-10);
						g.setFont(lehenetsia);
					 	g.setColor(kolorea);
					}}
				}
			}
		}
		if (this.loturaDu == false) {
			if (this.mota.compareTo("s")==0) {
				sd1.marraztu(g);
				this.ch.getBalioZuzenak()[this.getIzena()] = true;
			}
		} else if (this.loturaDu == true) {
			if (this.mota.compareTo("s")==0)
				sd1.ezabatu(g);
			this.ch.getBalioZuzenak()[this.getIzena()] = false;
		}
//		if(this.mota.compareTo("i")==0)
			if (exekutatuDa == true) {
				irt.marraztu(g);
			}
			
		
		
	}

	public boolean puntuBerdina(int x1, int y1) {
		// TODO Auto-generated method stub

		if ((y == y1) && (x == x1)) {
			return true;
		}
		return false;
	}

	public boolean areaEnComun(int x1, int y1) {
		// TODO Auto-generated method stub

		if ((y + 5 > y1) && (y - 5 < y1) && (x + 5 > x1) && (x - 5 < x1)) {
			return true;
		}
		return false;
	}

	public String getHankaMota() {
		return mota;
	}

	public void setPosizioa(int x, int y) {
		this.x = x;
		this.y = y;
		sd1.setPosizioa(x - 8, y - 8);
		irt.setPosizioa(x - 8, y - 8);
	}

	public int getIzena() {
		return izena;
	}

	public void setKoloreGorria() {
		kolorea = Color.red;
	}

	public void setKoloreBeltza() {
		// TODO Auto-generated method stub
		kolorea = Color.black;
	}

	// private void setLuzera(int luz){
	// luzera=luz;
	// }
	public void setX(int x1) {
		x = x1;
	}

	public int getX() {
		return x;
	}

	/**
	 * Devuelve la y de la esquina superior izquierda del rectangulo
	 * 
	 * @return y de la esquina superior izquierda del rectangulo
	 */
	public int getY() {
		return y;
	}

	public String getLogika() {
		return logika;
	}

	public void sarreraEzabatu() {
		sd1.loturaEzabatu();
	}

	public SarreraDatu getSarrera() {
		return sd1;
	}

	public IrteeraDatu getIrteera() {
		return irt;
	}

	public boolean getloturaDu() {
		return loturaDu;
	}

	public void setLoturaDu(boolean lotDu) {
		loturaDu = lotDu;
	}

	public void loturaGehitu(Loturak l) {
		lotLista.addElement(l);
	}

	public void setArea(boolean ar) {
		this.area = ar;
	}

	public void loturaEzabatu() {
		if (lotLista.size() > 0)
			lotLista.remove(0);
	}

	public Loturak lortuLotura() {
		Loturak l2 = null;
		if (lotLista.capacity() > indizea) {
			l2 = lotLista.firstElement();
			indizea++;

		}
		return l2;
	}

	public int putLoturaKopurua() {
		return lotLista.capacity();
	}

	public Vector<Loturak> getLotLista() {
		return lotLista;
	}
	public void setLogika(String log){
		logika=log;
	}
public boolean isExekutatua(){
	return exekutatuDa;
}
}
